<template>
  <div class="page-article" v-if="model">
    <div class="d-flex py-3 px-2 border-bottom text-blue" @click="$router.push('/')">
      <div class="iconfont icon-back"></div>
      <strong class="flex-1 pl-2 page-title">{{ model.title }}</strong>
      <div class="text-grey fs-xs">2021-03-10</div>
    </div>
    <!-- 文章主体 -->
    <div v-html="model.body" class="px-3 fs-lg body"></div>
    <div class="px-3 py-3 border-top">
      <div class="d-flex ai-center">
        <i class="iconfont icon-xiangguanzixunbeifen"></i>
        <strong class="text-blue fs-lg ml-1">相关资讯</strong>
      </div>
      <div class="pt-2">
        <router-link
          class="py-1"
          :to="`/articles/${item._id}`"
          tag="div"
          v-for="item in model.related"
          :key="item._id"
          >{{ item.title }}</router-link
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    id: { required: true },
  },
  data() {
    return {
      model: null,
    };
  },
  // 点击相关资讯跳转的同一个 地址 和 使用相同的组件 不会 触发 created()
  watch: {
    // id 变化就给定一个 methods   两种写法
    id: "fetch",
    // id() {
    //   this.fetch();
    // },
  },
  methods: {
    async fetch() {
      const res = await this.$http.get(`articles/${this.id}`);
      this.model = res.data;
      // console.log(res);
      // console.log(this.$route);
    },
  },
  created() {
    this.fetch();
  },
};
</script>

<style lang="scss">
.page-article {
  .body {
    iframe {
      width: 100%;
      height: auto;
    }

    img {
      max-width: 100%;
      height: auto;
    }
  }

  .page-title {
    padding-right: 1rem;
    white-space: nowrap;
    overflow: hidden;
    // -webkit-line-clamp: 1;
    text-overflow: ellipsis;
  }
}
</style>
